<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
//for stock
//ini_set("display_errors",1);
//error_reporting(E_ALL);
echo '<pre>';

function group_assoc($array, $key) {
    $return = array();
    foreach($array as $v) {
        $return[$v[$key]][] = $v;
    }
    return $return;
}

function toArray($obj)
{
    if (is_object($obj)) $obj = (array)$obj;
    if (is_array($obj)) {
        $new = array();
        foreach ($obj as $key => $val) {
            $new[$key] = toArray($val);
        }
    } else {
        $new = $obj;
    }
    return $new;
}

require_once('simplesql.class.php5');

$ini = parse_ini_file("web.ini");
while(list($key,$value) = each($ini)){
	if($key == 'hostName'){
		$hostName = $value;
	}
	else if($key == 'userName'){
		$userName = $value;
	}
	else if($key == 'password'){
		$password = $value;
	}
	else if($key == 'dbName'){
		$dbName = $value;
	}
}

$db = new simplesql($hostName,$userName,$password,$dbName);
$db->query("SET NAMES utf8");

	$myarticles = file_get_contents('cache.txt');
	$objInfo = unserialize($myarticles);
	$arrInfo = toArray($objInfo);	
	//Group the requests by their account_id
	
	//get data group by SupplierName
	$new_list = group_assoc($arrInfo, 'Supplier');
	$arr_manufacturer = array();
	foreach($new_list as $key => $value){
		$supplier = $key;
		$manufacturer_name = $value[0]['SupplierName'];
		$arr_manufacturer[$key]=$manufacturer_name;
	}
	
	var_dump($arr_manufacturer);
	
	$action = isset($_GET['action'])?$_GET['action']:"insert";
	$error = false;
	$result = true;
	$db->query("BEGIN");
	foreach($arr_manufacturer as $key=>$value){
		$db->fetch_row('supplier = ' . $key,'manufacturer');
		$exists = $db->get_rows();		
		if(empty($exists)){
			$fields = array('supplier', 'name', 'status');
			$data = array($key, "T - " . $value, '0');
			$manufacturer_id = $db->insert($fields, $data, 'manufacturer');
			if(!$manufacturer_id){
				$error = true;
				break;
			}
			$fields = array('manufacturer_id', 'store_id');
			$data = array($manufacturer_id, '0');
			$db->insert($fields, $data, 'manufacturer_to_store');
			
		}else{
			$fields = array( 'name');
			$data = array( "T - " . $value);
			
			$db->update($fields, $data, 'supplier = ' . $key, 'manufacturer');
		}
	}
	/*
	foreach($arr_manufacturer as $key=>$value){
		$db->fetch_row('supplier = ' . $key,'manufacturer');
		$exists = $db->get_rows();		
		if(empty($exists)){
			$fields = array('supplier', 'name', 'status');
			$data = array($key, $value, '0');
			$manufacturer_id = $db->insert($fields, $data, 'manufacturer');
			if(!$manufacturer_id){
				$error = true;
				break;
			}
			$fields = array('manufacturer_id', 'store_id');
			$data = array($manufacturer_id, '0');
			$db->insert($fields, $data, 'manufacturer_to_store');
			
		}
	}
	*/
	/*
	switch ($action){
		case 'insert':
			foreach($arr_manufacturer as $key=>$value){
				$db->fetch_row('supplier = ' . $key,'manufacturer');
				$exists = $db->get_rows();		
				if(empty($exists)){
					$fields = array('supplier', 'name');
					$data = array($key, $value);
					$manufacturer_id = $db->insert($fields, $data, 'manufacturer');
					if(!$manufacturer_id){
						$error = true;
						break;
					}
					$fields = array('manufacturer_id', 'store_id');
					$data = array($manufacturer_id, '0');
					$db->insert($fields, $data, 'manufacturer_to_store');
					
				}
			}
			break;
		case 'update':
			foreach($arr_manufacturer as $key=>$value){
			
				$fields = array('name');
				$data = array($value);
				$result = $db->update($fields, $data, 'supplier = ' . $key, 'manufacturer');
				if(!$result){
					$error = true;
					break;
				}
			}
			break;
		default:
			break;
	}
	*/

	if(!$error){  
	//*** Commit Transaction ***//  
		$db->query("COMMIT");  
		echo "OK <br />";  
		
	}else{  
		//*** RollBack Transaction ***//  
		echo "Error:  [".$db->get_lastquery()."]" ."-" . mysql_error();  
		$db->query("ROLLBACK");  
	}
	echo '</pre>';


?>
</body>
</html>